/**
 * @description 修改卡片链接
 * @author Ling
 */

import { IDomEditor, DomEditor } from '@wangeditor/editor';
import { LinkCardElement } from '../../custom-types';
import { Transforms } from 'slate';


export async function updateLinkCard(
  editor: IDomEditor,
  url: string,
  text: string,
  target: string = '_blank',
  linkType?: string,
  articleId?: string,
) {
  console.log('修改卡片链接', url, text, target, linkType, articleId);
  if (!articleId) return;
  // 修改链接
  const props: Partial<LinkCardElement> = {
    type: 'link-card',
    linkType,
    link: url,
    title: text,
    target,
    articleId,
    children: [{ text: text }],
  };
  
  Transforms.setNodes(editor, props, {
    match: (n) => DomEditor.checkNodeType(n, 'link-card'),
  });
  editor.move(1);
  editor.restoreSelection();
  editor.updateView();
}